﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;
using System.Net;

namespace STEditor.Classes
{
    public class STFrame : BitmapSource
    {

        private BitmapImage _image;

        public STFrame()
        {
            _image = new BitmapImage();
        }

        public static STFrame CreateFromBitmapImage(BitmapImage image)
        {
            var frame = new STFrame();
            frame._image = image;
            return frame;
        }

        protected override System.Windows.Freezable CreateInstanceCore()
        {
            return _image.GetAsFrozen();
        }

        private Uri _source;
        public Uri Source
        {
            get { return _source; }
            set
            {
                _image.BeginInit();
                _image.StreamSource = LoadImageFromUri(value);
                _image.EndInit();
                _source = value;
            }
        }
        
        public bool Blocking { get; set; }

        private Stream LoadImageFromUri(Uri uri)
        {

            var wr = WebRequest.Create(uri);
            var response = wr.GetResponse();            
            using (var resStream = response.GetResponseStream())
            {                
                byte[] buffer = new byte[16 * 1024];
                MemoryStream ms = new MemoryStream();
                int read;
                while ((read = resStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                response.Close();
                return ms;
            }            

        }

        public override int PixelWidth
        {
            get { return _image.PixelWidth; }
        }

        public override int PixelHeight
        {
            get { return _image.PixelHeight; }
        }

        public override System.Windows.Media.PixelFormat Format
        {
            get { return _image.Format; }
        }

        public override double DpiX
        {
            get { return _image.DpiX; }
        }

        public override double DpiY
        {
            get { return _image.DpiY; }
        }

        public override BitmapPalette Palette
        {
            get { return _image.Palette; }
        }

        public override void CopyPixels(Array pixels, int stride, int offset)
        {
            _image.CopyPixels(pixels, stride, offset);
        }

        public override void CopyPixels(System.Windows.Int32Rect sourceRect, Array pixels, int stride, int offset)
        {
            _image.CopyPixels(sourceRect, pixels, stride, offset);
        }

        public override void CopyPixels(System.Windows.Int32Rect sourceRect, IntPtr buffer, int bufferSize, int stride)
        {
            _image.CopyPixels(sourceRect, buffer, bufferSize, stride);
        }

        public override bool IsDownloading
        {
            get { return _image.IsDownloading; }
        }

        public override event EventHandler<System.Windows.Media.ExceptionEventArgs> DecodeFailed;

        public override event EventHandler DownloadCompleted;
        public override event EventHandler<System.Windows.Media.ExceptionEventArgs> DownloadFailed;
        public override event EventHandler<DownloadProgressEventArgs> DownloadProgress;

    }
}
